//
//  AddressViewController.m
//  JackDeve
//
//  Created by apple on 17/5/9.
//  Copyright © 2017年 wkq. All rights reserved.
//

#import "AddressViewController.h"

@interface AddressViewController ()<UITableViewDelegate,UITableViewDataSource>
{
    AddressView * v;
    NSDictionary  * _dic;
    LGZAddress *d;
     CGFloat a;
    NSMutableArray * arr;
    NSDictionary  * _deletedic;
}
@end

@implementation AddressViewController
- (void)viewWillAppear:(BOOL)animated {
    // 发送请求
    AddressModel *addressTool = [[AddressModel alloc] init];
    [[AddressModel showData]getDataAndBlock];
    addressTool.block =^(NSDictionary * dic) {
        _dic =dic;
        
        d = [[LGZAddress alloc]init];
        
        [d yy_modelSetWithDictionary:dic];
        
        
        arr =[[NSMutableArray alloc]init];
        arr =[NSMutableArray arrayWithArray:d.data];
       
        
        
            dispatch_async(dispatch_get_main_queue(), ^{
                [v.table reloadData];
            });
    };


}
- (void)viewDidLoad {
    [super viewDidLoad];
  self.view.backgroundColor=[UIColor whiteColor];
    a = 1.0/736.0*SCREEN_HEIGHT;
    [self aa];
    [self initAddAddress];
   
}

-(void )aa{
    //右上管理按钮
    UIButton *rightBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 50*a, 20*a)];
    [rightBtn setTitle:@"管理" forState:UIControlStateNormal];
    [rightBtn setTitleColor:[UIColor colorWithRed:51/255.0 green:51/255.0 blue:51/255.0 alpha:1] forState:0];
    rightBtn.titleLabel.textAlignment = 2;
    [rightBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [rightBtn addTarget:self action:@selector(clickRight:) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:rightBtn];
    
    //创建一个左上添加按钮
    UIButton *navLeftBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 5, 20, 20)];
    [navLeftBtn setBackgroundImage:[UIImage imageNamed:@"左箭头"] forState:UIControlStateNormal];
    [navLeftBtn addTarget:self action:@selector(click) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:navLeftBtn];
    self.title = @"收货地址";
    v= [[AddressView alloc]initWithFrame:self.view.bounds];
    self.view =v;
    v.table.delegate=self;
    v.table.dataSource =self;
}

//返回方法
-(void)click
{
    [self.navigationController popViewControllerAnimated:YES];
    
}
//删除管理
- (void)clickRight:(UIButton *)sender {
    [v.table setEditing:!v.table.editing animated:YES];
    if (v.table.editing) {
        [sender setTitle:@"完成" forState:UIControlStateNormal];
    } else {
        [sender setTitle:@"管理" forState:UIControlStateNormal];
    }
}
//删除方法
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
    
    //从数据源上删除
    
   [arr removeObjectAtIndex:indexPath.row];
    
    //从后台也删除
    [SVProgressHUD show];
    AddressModel * model =[[AddressModel alloc]init];
  [[AddressModel showData]getDataAndBlock1:[[d.data objectAtIndex:indexPath.row] address_id]];
   
    model.deleteblock =^(NSDictionary * dic) {
        _deletedic=dic;
       [self LoginHttpFinish];
    };
    
    [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationTop];
    
    [v.table setEditing:NO animated:YES];
    
}

#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return arr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSString *CellIdentifier = [NSString stringWithFormat:@"Cell%ld%ld", indexPath.section, indexPath.row];
    //出列可重用的cell ，通俗理解:从缓存池取标识为"Cell"的cell
  AddressTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (!cell) {
        cell = [[AddressTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }
    cell.model = arr[indexPath.row];
    cell.selectionStyle =UITableViewCellSelectionStyleNone;
    return cell;
}

#pragma mark - UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    AddData *model = arr[indexPath.row];
    return [tableView cellHeightForIndexPath:indexPath model:model keyPath:@"model" cellClass:[AddressTableViewCell class] contentViewWidth:SCREEN_WIDTH];
}

//添加收货地址
- (void)initAddAddress {
    UIButton *logOutBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, SCREEN_HEIGHT -60 , SCREEN_WIDTH, 60)];
    [logOutBtn setTitle:@"添加收货地址" forState:UIControlStateNormal];
    [logOutBtn setTitleColor:[UIColor colorWithRed:(233/255.0) green:(109/255.0) blue:(7/255.0) alpha:1] forState:UIControlStateNormal];
    [logOutBtn setBackgroundColor:[UIColor whiteColor]];
    [logOutBtn addTarget:self action:@selector(logOut) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:logOutBtn];
}
- (void)logOut {
    [self.navigationController pushViewController:[ZengAddressController new] animated:YES];
}
- (void)LoginHttpFinish
{
    if ([[_deletedic objectForKey:@"errmsg"] isEqualToString:@"need login"]) {
        dispatch_async(dispatch_get_main_queue(), ^{
            
            UIAlertController *alet = [UIAlertController alertControllerWithTitle:@"提示" message:@"请您登陆" preferredStyle:UIAlertControllerStyleAlert];
            UIAlertAction *act1 = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                
                LoginViewController * VC =[[LoginViewController alloc]init];
                [self.navigationController pushViewController:VC animated:YES];
                
            }];
            UIAlertAction *act2 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
            }];
            [alet addAction:act1];
            [alet addAction:act2];
            [self presentViewController:alet animated:YES completion:nil];
        });
    }else if ([[_deletedic objectForKey:@"errmsg"] isEqualToString:@"成功删除收货地址"])
    {
        
        dispatch_async(dispatch_get_main_queue(), ^{
            
            [SVProgressHUD dismiss];
            [SVProgressHUD showSuccessWithStatus:@"成功删除收货地址"];
            [SVProgressHUD dismissWithDelay:1.5];
    
        });
    }
   
    else
    {
        [SVProgressHUD dismiss];
        [SVProgressHUD showErrorWithStatus:@"删除收货地址失败"];
        [SVProgressHUD dismissWithDelay:1.5];
        
       
        
    }
}

@end
